package org.easyway.tiles.generic;

import java.awt.Color;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Exporter extends JDialog {

	private static final long serialVersionUID = -7188023628413803653L;

	private JPanel content = null;

	private JLabel lpath = null;

	private JLabel lrelative = null;

	private JTextField path = null;

	private JTextField prefixpath = null;

	private JButton ok = null;

	private JButton cancel = null;

	private JLabel lname = null;

	private JTextField filename = null;

	/**
	 * This is the default constructor
	 */
	public Exporter(JFrame owner) {
		super(owner, true);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(361, 176);
		this.setTitle("Exporter");
		this.setContentPane(getContent());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getContent() {
		if (content == null) {
			lname = new JLabel();
			lname.setBounds(new java.awt.Rectangle(20, 24, 117, 16));
			lname.setText("file name");
			lrelative = new JLabel();
			lrelative.setBounds(new java.awt.Rectangle(17, 76, 118, 16));
			lrelative.setText("image prefix path");
			lpath = new JLabel();
			lpath.setText("output path");
			lpath.setLocation(new java.awt.Point(16, 50));
			lpath.setSize(new java.awt.Dimension(79, 17));
			content = new JPanel();
			content.setLayout(null);
			content.add(lpath, java.awt.BorderLayout.NORTH);
			content.add(lrelative, null);
			content.add(getPath(), null);
			content.add(getPrefixpath(), null);
			content.add(getOk(), null);
			content.add(getCancel(), null);
			content.add(lname, null);
			content.add(getFilename(), null);
		}
		return content;
	}

	/**
	 * This method initializes path
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getPath() {
		if (path == null) {
			path = new JTextField();
			path.setBounds(new java.awt.Rectangle(161, 50, 174, 17));
			path.setText("C:/out/mylevel/");
			path.addCaretListener(new javax.swing.event.CaretListener() {
				public void caretUpdate(javax.swing.event.CaretEvent e) {
					if (path.getText().length() <= 0) {
						ok.setEnabled(false);
						path.setBackground(Color.PINK);
					} else {
						ok.setEnabled(false);
						if (filename.getBackground() == Color.WHITE)
							ok.setEnabled(true);

					}
				}
			});
			path.addCaretListener(new javax.swing.event.CaretListener() {
				public void caretUpdate(javax.swing.event.CaretEvent e) {
					if (filename.getText().length() <= 0) {
						ok.setEnabled(false);
						filename.setBackground(Color.PINK);
					} else {
						filename.setBackground(Color.WHITE);
						if (path.getBackground() == Color.WHITE)
							ok.setEnabled(true);
					}
				}
			});
		}
		return path;
	}

	/**
	 * This method initializes prefixpath
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getPrefixpath() {
		if (prefixpath == null) {
			prefixpath = new JTextField();
			prefixpath.setBounds(new java.awt.Rectangle(160, 76, 174, 16));
			prefixpath.setText("/images/tileset/");
		}
		return prefixpath;
	}

	/**
	 * This method initializes ok
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOk() {
		if (ok == null) {
			ok = new JButton();
			ok.setBounds(new java.awt.Rectangle(61, 109, 78, 22));
			ok.setText("OK");
			ok.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					boolean success = new File(path.getText()).mkdirs();
					if (!success) {
						// error
					}
					if (filename.getText().charAt(0) == '/') {
						filename.setText(filename.getText().substring(1));
					}
					if (path.getText().charAt(path.getText().length() - 1) != '/') {
						path.setText(path.getText() + '/');
					}
					if (prefixpath.getText().charAt(
							prefixpath.getText().length() - 1) != '/') {
						prefixpath.setText(prefixpath.getText() + '/');
					}
					if (prefixpath.getText().charAt(0) == '/') {
						prefixpath.setText(prefixpath.getText().substring(1));
					}
					success = new File(path.getText() + prefixpath.getText())
							.mkdirs();
					if (!success) {
						// error
					}
					OptimizedSaver.saveWorld(path.getText(),
							filename.getText(), prefixpath.getText());
					OptimizedSaver.saveTextures(path.getText()
							+ prefixpath.getText());
				}
			});
		}
		return ok;
	}

	/**
	 * This method initializes cancel
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCancel() {
		if (cancel == null) {
			cancel = new JButton();
			cancel.setBounds(new java.awt.Rectangle(159, 109, 78, 22));
			cancel.setText("Cancel");
			cancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setVisible(false);
				}
			});
		}
		return cancel;
	}

	/**
	 * This method initializes name
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getFilename() {
		if (filename == null) {
			filename = new JTextField();
			filename.setBounds(new java.awt.Rectangle(161, 24, 174, 17));
			filename.setText("myMap.map");
			filename.addCaretListener(new javax.swing.event.CaretListener() {
				public void caretUpdate(javax.swing.event.CaretEvent e) {
					System.out.println("caretUpdate()"); // TODO
					// Auto-generated
					// Event stub
					// caretUpdate()
				}
			});
		}
		return filename;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
